package com.shen.educenter.controller;


import com.shen.commonutils.JwtUtils;
import com.shen.commonutils.Result;
import com.shen.commonutils.ordervo.UcenterMemberOrder;
import com.shen.educenter.entity.UcenterMember;
import com.shen.educenter.entity.vo.RegisterVo;
import com.shen.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author shen
 * @since 2020-07-03
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping ("login")
    public Result loginUser(@RequestBody UcenterMember member){
    //调用service 里面的方法
        String token=ucenterMemberService.login(member);
        System.out.println(token);
        return  Result.ok().data("token",token);
    }
    //注册
    @PostMapping("register")

    public Result register(@RequestBody RegisterVo registerVo){

        ucenterMemberService.register(registerVo);

        return Result.ok();

    }
    //根据token获得用户信息
    @GetMapping("getMemberInfo")
    public  Result getMemberInfo(HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id得到用户信息
        UcenterMember member = ucenterMemberService.getById(id);
        return  Result.ok().data("userInfo",member);
    }

    @PostMapping("getUserInfoOrder/{id}")
    //ucenter 和order 公用一个返回对象 UcenterMemberOrder

    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){

        UcenterMember member = ucenterMemberService.getById(id);
        UcenterMemberOrder memberOrder=new UcenterMemberOrder();
        //赋值到 另一个对象
        BeanUtils.copyProperties(member,memberOrder);
        return memberOrder;
    }

    @GetMapping("countRegister/{day}")
    public Result countRegister( @PathVariable String day){
       Integer count= ucenterMemberService.countRegister(day);
        return Result.ok().data("countRegister",count);
    }

}

